package juc.lock;

import java.util.concurrent.TimeUnit;


public class Test1 {

    public static void main(String[] args) {


        Phone phone = new Phone();

        // synchronized方法 上锁的是调用者对象 phone
        new Thread(()->{
            phone.sendMsg();
        },"a").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
            phone.call();
        },"b").start();
    }


}


class Phone {
    public synchronized  void sendMsg() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }

    public synchronized void call() {
        System.out.println("打电话");
    }

    public  void sayHello() {
        System.out.println("hello");
    }
}